<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir."/inc/database.php";
class Adresse {
    
    private $id = "";
    private $vorname = "";
    private $nachname = "";
    private $name_show = 0;
    private $strasse = "";
    private $hausnummer = "";
    private $zusatz_hausnummer = "";
    private $plz = "";
    private $ort = "";
    private $adresse_show = 0;
    private $telefon = "";
    private $telefon_show = 0;
    private $tel_beruf = "";
    private $mobil = "";
    private $mobil_show = 0;
    private $email = "";
    private $email_show = 0;
    private $email_selten = 0;
    private $email_erlaubt = 0;
    private $anrede = "";
    private $deutsch = 1;
    private $beruf = "";
    private $linked_addres_id = 0;
    
    public function __construct($id='') {
        if($id!='') {
            $temp = Database::getDefault()->openId("adresse",$id);
            if($temp != null) {
                foreach($temp as $key => $value) {
                    $this->$key = $value;
                }
            }
        }
    }
    
    public function delete() {
        Database::getDefault()->deleteId("adresse",$this->id);
    }
    
    public function save() {
        $values = array();
        foreach ($this as $key => $value) {
            $values[$key] = $value;
        }
        if($this->id=="") {
            $this->id = Database::getDefault()->insert("adresse", $values);
        } else {
            Database::getDefault()->update("adresse", $values, $this->id);
        }
    }
    
    public function getLabel() {
        return $this->getVorname()." ".$this->getNachname()."\n".$this->getStrasse()." ".$this->getHausnummer().$this->getZusatzHausnummer()."\n".$this->getPlz()." ".$this->getOrt();
    }
    
    public function getId() {
        return $this->id;
    }
    
    public function setVorname($value) {
        $this->vorname = $value;
    }
    
    public function getVorname() {
        return $this->vorname;
    }
    
    public function setNachname($value) {
        $this->nachname = $value;
    }
    
    public function getNachname() {
        return $this->nachname;
    }
    
    public function setNameShow($value) {
        $this->name_show = $value;
    }
    
    public function isNameShow() {
        return $this->name_show;
    }
    
    public function setStrasse($value) {
        $this->strasse = $value;
    }
    
    public function getStrasse() {
        return $this->strasse;
    }
    
    public function setHausnummer($nummer, $zusatz) {
        $this->hausnummer = $nummer;
        $this->zusatz_hausnummer = $zusatz;
    }
    
    public function getHausnummer() {
        return $this->hausnummer;
    }
    
    public function getZusatzHausnummer() {
        return $this->zusatz_hausnummer;
    }
    
    public function setPlz($value) {
        $this->plz = $value;
    }
    
    public function getPlz() {
        return $this->plz;
    }
    
    public function setOrt($value) {
        $this->ort = $value;
    }
    
    public function getOrt() {
        return $this->ort;
    }
    
    public function setAdresseShow($value) {
        $this->adresse_show = $value;
    }
    
    public function isAdresseShow() {
        return $this->adresse_show;
    }
    
    public function setTelefon($value) {
        $this->telefon = $value;
    }
    
    public function getTelefon() {
        return $this->telefon;
    }
    
    public function setTelefonShow($value) {
        $this->telefon_show = $value;
    }
    
    public function isTelefonShow() {
        return $this->telefon_show;
    }
    
    public function setTelBeruf($value) {
        $this->tel_beruf = $value;
    }
    
    public function getTelBeruf() {
        return $this->tel_beruf;
    }
    
    public function setMobil($value) {
        $this->mobil = $value;
    }
    
    public function getMobil() {
        return $this->mobil;
    }
    
    public function setMobilShow($value) {
        $this->mobil_show = $value;
    }
    
    public function isMobilShow() {
        return $this->mobil_show;
    }
    
    public function setEMail($value) {
        $this->email = $value;
    }
    
    public function getEMail() {
        return $this->email;
    }
    
    public function setEMailShow($value) {
        $this->email_show = $value;
    }
    
    public function isEMailShow() {
        return $this->email_show;
    }
    
    public function setEMailSelten($value) {
        $this->email_selten = $value;
    }
    
    public function isEMailSelten() {
        return $this->email_selten;
    }
    
    public function setEMailErlaubt($value) {
        $this->email_erlaubt = $value;
    }
    
    public function isEMailErlaubt() {
        return $this->email_erlaubt;
    }
    
    public function setAnrede($value) {
        $this->anrede = $value;
    }
    
    public function getAnrede() {
        return $this->anrede;
    }
    
    public function setIsDeutsch($value) {
        $this->deutsch = $value;
    }
    
    public function isDeutsch() {
        return $this->deutsch;
    }

    public function setBeruf($value) {
        $this->beruf = $value;
    }
    
    public function getBeruf() {
        return $this->beruf;
    }
    
    public function getJsonData() {
        $var = get_object_vars($this);
        foreach($var as &$value) {
            if(is_object($value) && method_exists($value, 'getJsonData')) {
                $value = $value->getJsonData();
            } else {
                $value = utf8_encode($value);
            }
        }
        return $var;
    }
    
    static public function search($value) {
        global $VEREIN;
        $query = "SELECT DISTINCT * FROM " . $VEREIN['prefix'] . "adresse WHERE nachname LIKE '%".$value."%' OR vorname LIKE '%".$value."%' ORDER BY nachname, vorname";
        return Database::getDefault()->execute($query,"Adresse");
    }
    
    static public function getAll() {
        global $VEREIN;
        $query = "SELECT * FROM " . $VEREIN['prefix'] . "adresse ORDER BY nachname, vorname";
        return Database::getDefault()->execute($query,"Adresse");
    }
    
    /**
     * create table for addresses or update if something to do
     */
    public static function initDatabase() {
        global $VEREIN;
        echo "<b>create / update address table</b><br />";
        $query = "CREATE TABLE IF NOT EXISTS " . $VEREIN['prefix'] . "adresse (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY)";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            die(Database::getDefault()->getConnection()->error);
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN vorname VARCHAR(100) NOT NULL";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN nachname VARCHAR(50) NOT NULL";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN name_show TINYINT(1) NOT NULL DEFAULT 0";
        echo "- " . $query ."<br />";
                try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN strasse VARCHAR(100) DEFAULT NULL";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN hausnummer VARCHAR(5) DEFAULT NULL";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN zusatz_hausnummer VARCHAR(5) DEFAULT NULL";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN plz VARCHAR(7) DEFAULT NULL";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN ort VARCHAR(100) DEFAULT NULL";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN adresse_show TINYINT(1) NOT NULL DEFAULT 0";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN telefon VARCHAR(20) DEFAULT NULL";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN telefon_show TINYINT(1) NOT NULL DEFAULT 0";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN tel_beruf VARCHAR(20) DEFAULT NULL";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN mobil VARCHAR(20) DEFAULT NULL";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN mobil_show TINYINT(1) NOT NULL DEFAULT 0";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN email VARCHAR(250) DEFAULT NULL";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN email_show TINYINT(1) NOT NULL DEFAULT 0";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN email_selten TINYINT(1) NOT NULL DEFAULT 0";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN email_erlaubt TINYINT(1) NOT NULL DEFAULT 0";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN anrede VARCHAR(50) DEFAULT NULL";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN deutsch TINYINT(1) NOT NULL DEFAULT 0";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN beruf  VARCHAR(100) DEFAULT NULL";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        $query = "ALTER IGNORE TABLE " . $VEREIN['prefix'] . "adresse ADD COLUMN linked_addres_id INT UNSIGNED NOT NULL DEFAULT 0";
        echo "- " . $query ."<br />";
        try {
            $result = Database::getDefault()->executePrepare($query);
        } catch(Exception $e) {
            if(!$result && Database::getDefault()->getConnection()->errno != 1060) {
                die(Database::getDefault()->getConnection()->error);
            }
        }
        echo "<br />";
    }
}
?>